#include "listtablemodel.h"

#include <QFile>
#include <QDebug>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStringLiteral>

ListTableModel::ListTableModel(QObject *parent) : QAbstractTableModel(parent) {
    headers << QStringLiteral("选择") << QStringLiteral("订单编号")
            << QStringLiteral("商品名称") << QStringLiteral("数量")
            << QStringLiteral("需支付金额") << QStringLiteral("加入时间")
            << QStringLiteral("操作");
    parseJsonData(":/res/model_data.json");
}

int ListTableModel::rowCount(const QModelIndex &index) const {
    Q_UNUSED(index)
    return vDatas.size();
}

int ListTableModel::columnCount(const QModelIndex &index) const {
    Q_UNUSED(index)
    return headers.size();
}

QHash<int, QByteArray> ListTableModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[Role_is_selected] = "is_selected";
    roles[Role_order_num] = "order_num";
    roles[Role_product_name] = "product_name";
    roles[Role_product_total] = "product_total";
    roles[Role_product_price] = "product_price";
    roles[Role_join_time] = "join_time";
    return roles;
}

QVariant ListTableModel::data(const QModelIndex &index, int role) const {
    if (index.row() >= vDatas.size())
        return QVariant();
    const OrderData rowData = vDatas.at(index.row());
    switch (role) {
    case Role_is_selected:
        return rowData.is_selected;
    case Role_order_num:
        return rowData.order_num;
    case Role_product_name:
        return rowData.product_name;
    case Role_product_total:
        return rowData.product_total;
    case Role_product_price:
        return rowData.product_price;
    case Role_join_time:
        return rowData.join_time;
    default:
        return QVariant();
    }
}

bool ListTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    if (row < 0 || count < 1 || row + count > rowCount())
        return false;
    beginRemoveRows(parent, row, row + count - 1);
    for (int i = row + count - 1; i >= row; i--) {
        vDatas.removeAt(i);
    }
    endRemoveRows();
    return true;
}

void ListTableModel::parseJsonData(QString filePath)
{
    QFile file(filePath);
    if(!file.exists())
        return;
    if(!file.open(QFile::ReadOnly))
        return;
    QByteArray  jsonstr = file.readAll();
    QJsonParseError parseerror;
    QJsonDocument doc = QJsonDocument::fromJson(jsonstr, &parseerror);
    if(parseerror.error !=  QJsonParseError::NoError)
    {
        qDebug() <<"parseError";
        return;
    }
    QJsonObject obj = doc.object();
    QJsonArray data = obj["data"].toArray();
    foreach(auto item, data)
    {
        OrderData data;
        data.is_selected = item["is_selected"].toBool();
        data.order_num = item["order_num"].toString();
        data.product_name = item["product_name"].toString();
        data.product_total = item["product_total"].toInt();
        data.product_price = item["product_price"].toDouble();
        data.join_time = QDateTime::fromString(item["join_time"].toString(), "yyyy/MM/dd hh:mm");
        vDatas.push_back(data);
    }
}

void ListTableModel::remove(int row)
{
    removeRow(row);
}
